package com.njworkorder.Controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.DTO.AnnualplnDto;
import com.njworkorder.Entity.Annualpln;
import com.njworkorder.Service.AnnualplnService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Tag(name = "年计划相关接口", description = "年计划相关接口")
@RestController
@RequestMapping("/ann")
public class AnnualplnController {

    private final AnnualplnService annualplnService;
    public AnnualplnController(AnnualplnService annualplnService) {
        this.annualplnService = annualplnService;
    }


    @Operation(summary = "获取年计划配置列表")
    @GetMapping("/getDicList")
    public Result<Page<Annualpln>> getCIList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize) {
        LambdaQueryWrapper<Annualpln> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.apply("1=1");


        Page<Annualpln> page = new Page<>(pageIndex, pageSize);
        Page<Annualpln> page1 = annualplnService.page(page, queryWrapper);

        return ResultUtil.success(page1);
    }
    @Operation(summary = "新增年计划配置列表")
    @PostMapping("/AnInfo")
    public Result<String> addInfo(@RequestBody AnnualplnDto annualplnDto,
                                  @RequestHeader("Create-Ap-Id") String createApId){

        LambdaQueryWrapper<Annualpln> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Annualpln::getSectionId,annualplnDto.getSectionId());
        long count = annualplnService.count(lambdaQueryWrapper);
        if (count>0){
            return ResultUtil.fail("路段已存在!");
        }

        Annualpln an = new Annualpln();
        BeanUtils.copyProperties(annualplnDto,an);
        String id = UUID.randomUUID().toString();
        an.setId(id);
        an.setCreateApId(createApId);

        boolean save = annualplnService.save(an);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }
    @Operation(summary = "根据id删除年计划配置")
    @GetMapping("/deleteAnId")
    public Result<String> deleteManId(@RequestParam("id") String id){
        boolean removeById = annualplnService.removeById(id);
        if (removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }
    @Operation(summary = "根据id修改年计划配置")
    @PostMapping("/updateAnId/{id}")
    public Result<String> updateManId(@RequestBody AnnualplnDto annualplnDto,
                                      @PathVariable("id") String id){
        Annualpln An = new Annualpln();
        An.setId(id);

        BeanUtils.copyProperties(annualplnDto, An);

        boolean b = annualplnService.updateById(An);
        if (b){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }
   }